﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoryFlow
{
    public partial class ProjectConfigForm : Form
    {
        public ProjectConfigForm()
        {
            InitializeComponent();
        }

        private void ProjectConfigForm_Load(object sender, EventArgs e)
        {
            // 窗口加载，把当前项目的信息读入
            LoadCurrentProjectInfo();
        }

        #region 读取信息
        private void LoadCurrentProjectInfo()
        {
            // 判断如果当前项目不为空，那就读取信息
            if (Common.currentProject != null)
            {
                LoadCurrentProjectBasicInfo();
            }
            else
            {
                // 当前项目为空，弹窗提示
                MessageBox.Show("未打开任何项目！", "项目信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
                return;
            }
        }

        // 读取项目的基本信息
        private void LoadCurrentProjectBasicInfo()
        {
            editBasicTitle.Text = Common.currentProject.Title;
            editBasicAuthor.Text = Common.currentProject.Author;
            editBasicDescription.Text = Common.currentProject.Description;
            editBasicCategory.Text = Common.currentProject.Category;
        }
        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (true == SaveCurrentProjectInfo())
            {
                Close();
            }
            else
            {
                // 如果保存信息校验没有通过，则不关闭窗口
            }
        }

        #region 保存信息
        private bool SaveCurrentProjectInfo()
        {
            bool isOk = true;
            // 能打开这个窗口证明当前项目肯定不是空的，所以就不需要再这里判断是否为空了
            isOk &= SaveCurrentProjectBasicInfo();

            return isOk;
        }

        // 保存项目的基本信息
        private bool SaveCurrentProjectBasicInfo()
        {
            // 判断故事标题是否为空或空白字符
            if (editBasicTitle.Text == String.Empty)
            {
                // 弹窗提示故事标题不能是空
                MessageBox.Show("故事的标题不能为空", "项目信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else
            {
                Common.currentProject.Title = editBasicTitle.Text;
            }

            Common.currentProject.Description = editBasicDescription.Text;
            Common.currentProject.Author = editBasicAuthor.Text;
            Common.currentProject.Category = editBasicCategory.Text;

            return true;
        }
        #endregion
    }
}
